<template>
  <div class="roles">
    <el-row>
      <el-col :span="24">
        <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>权限管理</el-breadcrumb-item>
        <el-breadcrumb-item>角色列表</el-breadcrumb-item>
      </el-breadcrumb>
      </el-col>
    </el-row>
    <!-- 按钮 -->
    <el-button type="primary" plain>添加角色</el-button>
    <!-- 表格区域 -->
    <el-table
    class="mt15"
    border
    :data="rolesList"
    style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="scope">
          <!-- 三层循环渲染区域 -->
          <el-row v-for="(firstLayer, index) in scope.row.children" :key="index">
            <el-col :span="4">
              <el-tag closable @close="deleteRight(scope.row, firstLayer.id)">{{firstLayer.authName}}</el-tag>
              <i class="el-icon-arrow-right" v-if="firstLayer.children.length !== 0"></i>
            </el-col>
            <el-col :span="20">
              <el-row v-for="secondLayer in firstLayer.children" :key="secondLayer.id">
                  <el-col :span="4">
                    <el-tag closable @close="deleteRight(scope.row, secondLayer.id)">{{secondLayer.authName}}</el-tag>
                    <i class="el-icon-arrow-right" v-if="secondLayer.children.length !== 0"></i>
                  </el-col>
                  <el-col :span="20">
                    <el-tag closable type="warning" @close="deleteRight(scope.row, thirdLayer.id)"
                     v-for="thirdLayer in secondLayer.children" :key="thirdLayer.id">
                    {{thirdLayer.authName}}
                    </el-tag>
                  </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-row v-if="scope.row.children.length == 0">
            <el-col :span="24">该角色没有权限分配，请前往分配。。。</el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column
        label="角色名称"
        prop="roleName">
      </el-table-column>
      <el-table-column
        label="描述"
        prop="roleDesc">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain icon="el-icon-edit"></el-button>
          <el-button size="mini" type="danger" plain icon="el-icon-delete"></el-button>
          <el-button size="mini" type="warning" plain icon="el-icon-check" title="授权角色" @click="showDialog(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 1. 授权角色弹窗 -->
    <el-dialog
      title="授权角色"
      :visible.sync="dialogVisible"
      width="30%">
      <div  class="tree-container">
        <el-tree
          :data="rightList"
          show-checkbox
          node-key="id"
          ref="tree"
          :default-expand-all="true"
          :default-checked-keys="selectedRights"
          :props="defaultProps">
        </el-tree>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitGrant">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {getRoleList, deleteRoleRight, getRightList, grantRoleRight} from '@/api'
export default {
  data() {
    return {
      rolesList: [],
      dialogVisible: false,
      rightList: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      selectedRights: [], // 保存默认选中的权限id
      currentRole: {}  // 保存点击的角色
    }
  },
  created() {
    this.initList()
  },
  methods: {
    initList() {
      getRoleList().then(res => { // 初始获取数据方法
        if (res.meta.status === 200) {
          // console.log(res)
          this.rolesList = res.data
        }
      })
    },
    deleteRight(row, rightId) { // 删除角色权限方法
      // console.log(row, rightId)
      deleteRoleRight({roleId: row.id, rightId: rightId}).then(res => {
        if (res.meta.status === 200) {
          row.children = res.data
        } else {
          this.$message({
            type: 'error',
            message: res.meta.msg
          })
        }
      })
    },
    showDialog(row) { // 展示授权角色弹窗方法
      this.dialogVisible = true
      this.currentRole = row
      console.log(row.children) // [{authName:"商品管理",children:Array(3),id:101,path:"goods",pid:0},{//...}]
      getRightList({type: 'tree'}).then(res => {
        if (res.meta.status === 200) {
          this.rightList = res.data
        } else {
          this.$message({
            type: 'error',
            message: res.meta.msg
          })
        }
      })

      /* // 遍历之前先让数组清空
      this.selectedRights.length = 0
      // 取出当前点击角色的所有权限， 然后遍历到它的第三个children，取出它里面的所有的项的id，让进selectedRights中
      this.currentRole.children.forEach(first => {
        if (first.children && first.children.length !== 0) {
          first.children.forEach(second => {
            if (second.children && second.children.length !== 0) {
              second.children.forEach(third => {
                this.selectedRights.push(third.id)
              })
            }
          })
        }
      }) */
      // 注意 只需要遍历数据  拿到最外层的id 最外层选中之后 他下面的书都会被选中
      // 不需要向上面那样这么麻烦。。。
      this.selectedRights.length = 0
      row.children.forEach(item => {
        this.selectedRights.push(item.id)
      })
      console.log(this.selectedRights)
    },
    submitGrant () { // 提交授权
      let rids = this.$refs.tree.getCheckedKeys().join(',')
      grantRoleRight(this.currentRole.id, {rids: rids}).then(res => {
        if (res.meta.status === 200) {
          this.$message({
            type: 'success',
            message: res.meta.msg
          })
          this.dialogVisible = false
          this.initList()
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.roles {
  .el-tag {
    margin-right: 5px;
    margin-bottom: 5px;
  }
  .tree-container {
    height: 300px;
    overflow: auto;
  }
}
</style>
